package game.mechanics;

import java.util.HashSet;
import java.util.Set;

import game.map.MapUnit;
import game.units.Character;
import game.units.Unit;
import game.util.Die;

/**
 * Define actions done on a game turn, and handles possible actions.
 * Particular of D&D 3.5 
 * @author Cyro
 *
 */
public class GameTurn {
	
	public enum TurnAction { ATTACK, MOVE, DOOR, MAGIC, EQUIP, USE_ITEM, PICK_ITEM };
	
	private MapUnit mapUnit;
	
	private Set<TurnAction> actionsMade;
	private int actionCounters;
	private boolean onTheMove;
	
	public GameTurn(MapUnit mu) {
		mapUnit = mu;
		actionCounters = 2;
		onTheMove = false;
		actionsMade = new HashSet<TurnAction>();
	}
	
	/**
	 * @return willPlayTurn
	 */
	public boolean initPhase() {
		Unit unit = mapUnit.getUnit();
		
		if (unit instanceof Character) {
			Character wsChar = (Character) unit;
			if (wsChar.getCurrentHP() < 0) {
				if (wsChar.isBleeding()) {
					// test fortitude
					if (Die.roll(20, 1) <= 2 + wsChar.getFortitude())
						wsChar.setBleeding(false);
					else {
						wsChar.alterCurrenHP(-1);
						if (wsChar.getCurrentHP() <= -10)
							mapUnit.setDead(true);
					}
				}
				return false;
			}
		} // else { // it's a monstah! }
		return true;
	}
	
	public boolean action(TurnAction act) {
		
		if (onTheMove) {
			if (act == TurnAction.MOVE)
				return true;
			else if (actionCounters != 0) {
				if (!mapUnit.canStayThere())
					return false;
				onTheMove = false;
				mapUnit.commitMove();
			}
		}
		
		if (actionCounters == 0) {
			System.out.println("You can only take 2 different actions per turn.");
			return false;
		}
		
		if (actionsMade.contains(act)) {
			System.out.println("You've already taken that kind of action this turn.");
			return false;
		}
		
		switch (act) {
		case ATTACK:
			break;
		case MOVE:
			onTheMove = true;
			break;
		}
		
		actionsMade.add(act);
		actionCounters--;
		return true;
		
	}

	public void undoMoves() {
		
		if (!onTheMove)
			return;
		
		onTheMove = false;
		actionsMade.remove(TurnAction.MOVE);
		actionCounters++;
		
	}
	
}
